from django_excel_templates import *
from django_excel_templates.color_converter import *
from models import *
from django.http import HttpResponse

def xls_view(request):
    """
    test_instance = TestAllBaseTypes(
    BooleanField = True,
    CharField = 'Testing CharField...',
    CommaSeparatedIntegerField = '2,8,9',
    DecimalField = '8.23',
    EmailField = 'chalkdust1012@gmail.com',
    FloatField = float(42),
    IntegerField = 9,
    IPAddressField = '192.168.4.180',
    NullBooleanField = False,
    PositiveIntegerField = 42,
    PositiveSmallIntegerField = 12,
    SlugField = 'testingslugfield',
    SmallIntegerField = 1,
    TextField = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer neque leo, viverra et convallis et, dictum vel quam. Aliquam a quam eros. Suspendisse imperdiet sapien non orci placerat sed faucibus odio vulputate. Nulla a lacus eros, quis volutpat metus. Nunc vulputate bibendum lectus vitae malesuada. Mauris hendrerit euismod accumsan. Etiam in eros et nisl ultrices cursus vel non nisl. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In pretium nunc sed nisi porta eget gravidaquam aliquam. Aliquam sollicitudin leo eu arcu imperdiet a ultrices erat porttitor. Etiam rutrum laoreet purus, a viverra odio hendreritsit amet.",
    URLField = 'www.google.com',
    )
    
    test_instance.save()

    testobj = TestAllBaseTypes.objects.all()

    headerstyle = ExcelStyle()
    headerstyle.set_font(font_color='FFFFFF',bold=True)
    headerstyle.set_border(border_color='000000',border_style=5)
    headerstyle.set_pattern(pattern_color='000000',pattern=1)
    bodystyle = ExcelStyle(font_color='C0C0C0')
    altcolorstyle = ExcelStyle(font_color='00000',pattern_color='C0C0C0',pattern=1,)
    
    formatter = ExcelFormatter()
    formatter.addHeaderStyle(headerstyle)
    formatter.addBodyStyle(bodystyle)
    formatter.addAlternateColorStyle(altcolorstyle)
    formatter.addFilter(include='SmallIntegerField,PositiveIntegerField,IntegerField,DateField,DecimalField')
    formatter.addFilter(exclude='DateField,FloatField,SlugField')
    formatter.addFormula('IntegerField','SUM')
    formatter.addFormula('DateField','COUNT')
    formatter.addFormula('SmallIntegerField,PositiveIntegerField','MAX')
    formatter.addFormula('DecimalField','MIN')
    
    ## Simple ##
    testobj = Book.objects.all()
    
    simpleFormatter = ExcelFormatter()
    simpleStyle = ExcelStyle(vert=2,wrap=1)
    simpleFormatter.addBodyStyle(simpleStyle)
    simpleFormatter.setWidth('name,category,publish_date,bought_on',3000)
    simpleFormatter.setWidth('price',600)
    simpleFormatter.setWidth('ebook',1200)
    simpleFormatter.setWidth('about',20000)
    
    simpleReport = ExcelReport()
    simpleReport.addSheet("TestSimple")
    simpleReport.addFilter(order='name,category,publish_date,about,bought_on,price,ebook')
    simpleReport.addFormatter(simpleFormatter)
    simpleReport.addQuerySet(testobj,REPORT_HORZ,True)
    """
    ## Medium ##
    mediumReport = ExcelReport()
    mediumReport.addSheet("TestMedium")
    testobj = Book.objects.all()

    headerstyle = ExcelStyle(font_color='FFFFFF',bold=True,pattern_color='000080',pattern=1)
    bodystyle = ExcelStyle(pattern_color='0066CC',pattern=1,vert=2,wrap=1,border_color='000000',border_style=2)
    colstyle = ExcelStyle(pattern_color='FF00FF',pattern=1,vert=2,wrap=1,border_color='FFFFFF',border_style=2)
    
    mediumFormatter = ExcelFormatter()
    mediumFormatter.addHeaderStyle(headerstyle)
    mediumFormatter.addBodyStyle(bodystyle)
    mediumFormatter.addColumnStyle('chapters',colstyle)
    mediumFormatter.setWidth('name,category,publish_date,bought_on',3200)
    mediumFormatter.setWidth('chapters',10000)
    mediumFormatter.setWidth('price',600)
    mediumFormatter.setWidth('ebook',1200)
    mediumFormatter.setWidth('about',20000)
    
    mediumReport.addFilter(order='name,category,publish_date,about,bought_on,price,ebook,chapters')
    mediumReport.addFormatter(mediumFormatter)
    mediumReport.addQuerySet(testobj,REPORT_HORZ,True)

    #response = HttpResponse(simpleReport.writeReport(),mimetype='application/ms-excel')
    response = HttpResponse(mediumReport.writeReport(),mimetype='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename=test.xls'
    return response


def Break():
      import dbgp.client
      dbgp.client.brk(port=9000)
